<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员仪表盘 - 教学管理系统</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
        }

        .container {
            display: flex;
            min-height: 100vh;
        }

        .sidebar {
            width: 250px;
            background-color: #2c3e50;
            color: white;
            padding-top: 20px;
        }

        .sidebar-header {
            padding: 0 20px 20px;
            border-bottom: 1px solid #3c5166;
        }

        .sidebar-header h2 {
            margin: 0;
            font-size: 20px;
        }

        .user-info {
            margin-top: 10px;
            font-size: 14px;
            opacity: 0.8;
        }

        .menu {
            list-style: none;
            padding: 0;
            margin: 20px 0;
        }

        .menu li {
            padding: 12px 20px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .menu li:hover {
            background-color: #3c5166;
        }

        .menu li.active {
            background-color: #3498db;
        }

        .menu a {
            color: white;
            text-decoration: none;
            display: block;
        }

        .menu-section {
            padding: 10px 20px;
            font-size: 14px;
            color: rgba(255, 255, 255, 0.6);
            border-bottom: 1px solid #3c5166;
            margin-top: 15px;
        }

        .content {
            flex: 1;
            padding: 20px;
        }

        .content-header {
            margin-bottom: 30px;
        }

        .content-header h1 {
            margin: 0;
            color: #333;
            font-size: 24px;
        }

        .dashboard-cards {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
        }

        .card {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
        }

        .card h2 {
            margin-top: 0;
            color: #333;
            font-size: 18px;
        }

        .card p {
            color: #666;
        }

        .card-stat {
            font-size: 24px;
            font-weight: bold;
            color: #3498db;
            margin-top: 10px;
        }

        .logout {
            margin-top: auto;
            padding: 20px;
        }

        .logout a {
            color: #e74c3c;
            text-decoration: none;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="sidebar">
            <div class="sidebar-header">
                <h2>教学管理系统</h2>
                <div class="user-info" th:if="${session.user}">
                    <span th:text="${session.user.username}">admin1</span> - 管理员
                </div>
            </div>
            <div class="menu-section">基础管理</div>
            <ul class="menu">
                <li class="active"><a href="/admin/dashboard">系统概览</a></li>
                <li><a href="/admin/teachers">教师管理</a></li>
                <li><a href="/admin/students">学生管理</a></li>
                <li><a href="/admin/courses">课程管理</a></li>
                <li><a href="/admin/classrooms">教室管理</a></li>
            </ul>

            <div class="menu-section">教务管理</div>
            <ul class="menu">
                <li><a href="/admin/course-selections">选课管理</a></li>
                <li><a href="/admin/grades">成绩管理</a></li>
            </ul>

            <div class="menu-section">系统设置</div>
            <ul class="menu">
                <li><a href="/admin/profile">个人信息</a></li>
            </ul>

            <div class="logout">
                <a href="/logout">退出登录</a>
            </div>
        </div>

        <div class="content">
            <div class="content-header">
                <h1>系统概览</h1>
            </div>

            <div class="dashboard-cards">
                <div class="card">
                    <h2>教师数量</h2>
                    <div class="card-stat" id="teacherCount">0</div>
                    <p>系统中所有注册教师的数量</p>
                </div>
                <div class="card">
                    <h2>学生数量</h2>
                    <div class="card-stat" id="studentCount">0</div>
                    <p>系统中所有注册学生的数量</p>
                </div>
                <div class="card">
                    <h2>课程数量</h2>
                    <div class="card-stat" id="courseCount">0</div>
                    <p>系统中开设的所有课程数量</p>
                </div>
                <div class="card">
                    <h2>教室数量</h2>
                    <div class="card-stat" id="classroomCount">0</div>
                    <p>系统中所有可用教室的数量</p>
                </div>
                <div class="card">
                    <h2>选课记录</h2>
                    <div class="card-stat" id="courseSelectionCount">0</div>
                    <p>系统中所有选课记录的数量</p>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 页面加载时获取统计数据
        document.addEventListener('DOMContentLoaded', function () {
            fetchDashboardData();
        });

        // 获取仪表盘数据
        function fetchDashboardData() {
            // 获取教师数量
            fetch('/api/admin/teachers')
                .then(response => {
                    if (!response.ok) {
                        if (response.status === 401) {
                            alert('未授权，请重新登录');
                            window.location.href = '/login';
                        }
                        throw new Error('获取教师数据失败');
                    }
                    return response.json();
                })
                .then(data => {
                    document.getElementById('teacherCount').textContent = data.length;
                })
                .catch(error => {
                    console.error('教师数据加载失败:', error);
                });

            // 获取学生数量
            fetch('/api/admin/students')
                .then(response => {
                    if (!response.ok) {
                        throw new Error('获取学生数据失败');
                    }
                    return response.json();
                })
                .then(data => {
                    document.getElementById('studentCount').textContent = data.length;
                })
                .catch(error => {
                    console.error('学生数据加载失败:', error);
                });

            // 获取课程数量
            fetch('/api/admin/courses')
                .then(response => {
                    if (!response.ok) {
                        throw new Error('获取课程数据失败');
                    }
                    return response.json();
                })
                .then(data => {
                    document.getElementById('courseCount').textContent = data.length;
                })
                .catch(error => {
                    console.error('课程数据加载失败:', error);
                });

            // 获取教室数量
            fetch('/api/admin/classrooms')
                .then(response => {
                    if (!response.ok) {
                        throw new Error('获取教室数据失败');
                    }
                    return response.json();
                })
                .then(data => {
                    document.getElementById('classroomCount').textContent = data.length;
                })
                .catch(error => {
                    console.error('教室数据加载失败:', error);
                });

            // 获取选课记录数量
            fetch('/api/admin/course-selections')
                .then(response => {
                    if (!response.ok) {
                        throw new Error('获取选课数据失败');
                    }
                    return response.json();
                })
                .then(data => {
                    document.getElementById('courseSelectionCount').textContent = data.length;
                })
                .catch(error => {
                    console.error('选课数据加载失败:', error);
                    // 如果API尚未实现，不显示错误
                    document.getElementById('courseSelectionCount').textContent = '-';
                });
        }
    </script>
</body>

</html>